﻿
using System;
using System.Collections.Generic;
using NHibernate;
using NHibernate.Cfg;

namespace CreaMotion
{
    public class NHibernateSessionManager
    {

        #region Properties & Assembly
        
        public static Dictionary<string, string> NHibernatenProperties;
        public static string NHibernateAssembly;

        #endregion 

        #region ISessionFactory

        private static ISessionFactory sessionFactory;
        public static ISessionFactory SessionFactory
        {
           get
            {
                if (sessionFactory == null)
                    sessionFactory = CreateConfiguration().BuildSessionFactory();
                
                return sessionFactory;
            }
        }

        #endregion

        private static Configuration CreateConfiguration()
        {
            Configuration config = new Configuration();
            config.SetProperties(NHibernatenProperties);
            config.AddAssembly(NHibernateAssembly);

            return config;
        }

        public static ISession CreateSession(Dictionary<string, string> properties, string assembly)
        {
            NHibernatenProperties = properties;
            NHibernateAssembly = assembly;
            return SessionFactory.OpenSession();
        }
    }
}
